service = new Services(txtIP.Text,Convert.ToInt32(txtPort.Text));
sSocket = service.StartServiceListen();
//监听端口
new Task(() => {
while (true)
{
//等待连接
Socket socket= sSocket.Accept();
appendTextInRichText(socket.RemoteEndPoint.ToString() ": 已连接");
AddLineUserInfo(socket);
//接收消息
new Task(()=> {
while (true)
{
byte[] buff = new byte[1024 * 1024 * 2];
int r = socket.Receive(buff);
if (r==0)
{
continue;
}
string message = Encoding.UTF8.GetString(buff, 1, r - 1);
MessageTypeEnum mte = (MessageTypeEnum)Enum.Parse(typeof(MessageTypeEnum), buff[0].ToString());
appendTextInRichText("**** " socket.RemoteEndPoint ":" mte.ToString() " ****");
switch (mte)
{
case MessageTypeEnum.获取附近的人:
GetNearUser(socket);
break;
case MessageTypeEnum.设置个人信息:
SetPersonalInformation(message);
break;
case MessageTypeEnum.服务器收文字:
ServerReceiveText(socket, message);
break;
case MessageTypeEnum.服务器收振动:
ServerReceiveVibration(socket);
break;
case MessageTypeEnum.服务器收文件:
SendFileToService(buff);
break;
case MessageTypeEnum.服务器转发文字:
TransmitText(socket, message.Split('|'));
break;
case MessageTypeEnum.服务器转发振动:
TransmitVibration(socket, message);
break;
case MessageTypeEnum.服务器转发文件:
TransmitFile(socket, buff);
break;
default:
break;
}
}
}).Start();
}
}).Start();
评论